import { sys } from "cc";
import SdkTool from "../Sdk/SdkTool";
import { EnumplatFrom } from "../Sdk/EnumplatFrom";

export const enum IHttpMethod {
    GET = "get",
    POST = "post"
}

export default class Http {
    private static instance: Http;
    public static get ins(): Http {
        if (!Http.instance) {
            Http.instance = new Http();
        }
        return Http.instance;
    }
    /**
     * 请求
     * @param url 链接
     * @param param 参数
     * @param callback 回调
     * @param method 方案
     * @param isToken 是否从缓存中写入token文件
     */
    public request(url: string, param: any, callback: (data: any) => void, method: IHttpMethod = IHttpMethod.POST): void {
        switch (method) {
            case IHttpMethod.GET:
                this.getRequest(url, param, callback);
                break;
            case IHttpMethod.POST:
                this.postRequest(url, param, callback);
                break;
        }
    }

    /**
     * GET
     * @param url 链接
     * @param param 参数
     * @param callback 回调
     * @param https 是否使用 https协议
     */
    private getRequest(url: string, param: Object, callback: (data: any) => void): void {
        let request = new XMLHttpRequest();
        request.open(IHttpMethod.GET, url, true);
        this.setRequestHeader(request);
        request.onreadystatechange = () => {
            if (request.readyState == 4) {
                if (request.status >= 200 && request.status < 400) {
                    callback(request.responseText);
                } else {
                    console.log(`url:(${url}) request error. status:(${request.status})`);
                    callback(null);
                }
            }
        };
        request.send();
    }

    /**
     * POST
     * @param url 链接
     * @param param 参数
     * @param callback 回调
     * @param https 是否使用 https协议
     */
    private postRequest(url: string, param: any, callback: (data: any) => void): void {
        let request = new XMLHttpRequest();
        request.open(IHttpMethod.POST, url);
        this.setRequestHeader(request);
        request.onreadystatechange = () => {
            if (request.readyState == 4) {
                if (request.status >= 200 && request.status < 400) {
                    callback(request.responseText);
                } else {
                    console.log(`${url}请求失败:${request.status}`);
                    callback(null);
                }
            }
        };
        request.send(param);
    }
    /**
     * 设置http头
     * @param request XMLHttpRequest
     */
    private setRequestHeader(request: XMLHttpRequest): void {
        request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8;application/x-www-form-urlencoded;application/json");
        request.setRequestHeader("Cache-Control", "no-store");
        request.setRequestHeader("Access-Control-Allow-Origin", "*");
        request.setRequestHeader("Access-Control-Allow-Credentials", `true`);
    }

    /**
     * 获取请求链接
     * @param url 链接
     * @param https 是否使用 https协议
     * @returns {string} 
     */
    // private getUrl(url: string, https: boolean): string {
    //     if (https) {
    //         if (url.indexOf("https") == -1) {
    //             url = url.replace("http", "https");
    //         }
    //     }
    //     return url;
    // }
}